{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "af55578a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "#import seaborn as sns\n",
    "import math\n",
    "from sklearn import metrics\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "e03e6dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_excel(\"./深度学习多分类.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "1ec4aeb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>toe_force</th>\n",
       "      <th>heel_force</th>\n",
       "      <th>knee_angle</th>\n",
       "      <th>thigh_velocity</th>\n",
       "      <th>leg_velocity</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>-25</td>\n",
       "      <td>-26</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>6</td>\n",
       "      <td>-30</td>\n",
       "      <td>-29</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>32</td>\n",
       "      <td>6</td>\n",
       "      <td>-29</td>\n",
       "      <td>-25</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>49</td>\n",
       "      <td>6</td>\n",
       "      <td>-26</td>\n",
       "      <td>-27</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>58</td>\n",
       "      <td>7</td>\n",
       "      <td>-23</td>\n",
       "      <td>-32</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2204</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>-70</td>\n",
       "      <td>-23</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2205</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>-67</td>\n",
       "      <td>-46</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2206</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>-53</td>\n",
       "      <td>-73</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2207</th>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>27</td>\n",
       "      <td>-10</td>\n",
       "      <td>-82</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2208</th>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>33</td>\n",
       "      <td>40</td>\n",
       "      <td>-94</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2209 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      toe_force  heel_force  knee_angle  thigh_velocity  leg_velocity  label\n",
       "0             0           0           6             -25           -26      0\n",
       "1             0          22           6             -30           -29      0\n",
       "2             0          32           6             -29           -25      0\n",
       "3             0          49           6             -26           -27      0\n",
       "4             0          58           7             -23           -32      0\n",
       "...         ...         ...         ...             ...           ...    ...\n",
       "2204          1           0          20             -70           -23      4\n",
       "2205          5           0          20             -67           -46      4\n",
       "2206          3           0          23             -53           -73      4\n",
       "2207         22           0          27             -10           -82      4\n",
       "2208         27           0          33              40           -94      4\n",
       "\n",
       "[2209 rows x 6 columns]"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "9dbe7698",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [1., 0., 0., 0., 0.],\n",
       "       [1., 0., 0., 0., 0.],\n",
       "       ...,\n",
       "       [0., 0., 0., 0., 1.],\n",
       "       [0., 0., 0., 0., 1.],\n",
       "       [0., 0., 0., 0., 1.]], dtype=float32)"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from keras.utils.np_utils import to_categorical\n",
    "one_hot_test_labels=to_categorical(df.iloc[:,-1])\n",
    "one_hot_test_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "96a30b14",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示负号\n",
    "X= df.iloc[:,:-1]\n",
    "#Y = df.iloc[:,-1]\n",
    "#特征缩放\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, one_hot_test_labels, test_size=0.2)\n",
    "scaler=StandardScaler()\n",
    "x_train=scaler.fit_transform(x_train)\n",
    "x_test=scaler.fit_transform(x_test)\n",
    "\n",
    "x_train,y_train=np.array(x_train),np.array(y_train)\n",
    "x_train=np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))\n",
    "x_test,y_test=np.array(x_test),np.array(y_test)\n",
    "x_test = np.reshape(x_test, (x_test.shape[0],x_test.shape[1],1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "4be7b349",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 1.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       ...,\n",
       "       [1., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 1., 0., 0., 0.]], dtype=float32)"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "55dd16fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "              # -*- coding: utf-8 -*-\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "import time     \n",
    "#from tensorflow.keras.optimizers import SGD\n",
    "from tensorflow.python.keras.optimizer_v2.gradient_descent import SGD \n",
    "from keras.layers import Input, Dense\n",
    "from keras.models import Model\n",
    "from keras.layers import *\n",
    "from keras.models import *\n",
    "from keras.optimizer_v2 import adam\n",
    "#from keras.optimizers import adam_v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "784df6f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1767, 5, 1) (1767, 5) (442, 5, 1) (442, 5)\n"
     ]
    }
   ],
   "source": [
    "print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "6da2bbcb",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_dim = 1#输出\n",
    "batch_size = 50\n",
    "epochs = 200#迭代次数\n",
    "TIME_STEPS= 1\n",
    "hidden_size = 128\n",
    "INPUT_DIM = 9\n",
    "lstm_units = 64\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "528825fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, 1, 128)\n"
     ]
    }
   ],
   "source": [
    "#inputs = Input(shape=(TIME_STEPS, INPUT_DIM))\n",
    "inputs = Input(shape=(5, 1))\n",
    "#drop1 = Dropout(0.3)(inputs)\n",
    "\n",
    "#x = Conv1D(filters = 9, kernel_size = 1, activation = 'relu')(inputs)  #, padding = 'same'\n",
    "x = Conv1D(filters=64, kernel_size=5, activation='relu')(inputs)#embedded_sequences\n",
    "x = Conv1D(filters=128, kernel_size=5, activation='relu')(inputs)#embedded_sequences\n",
    "x = MaxPooling1D(pool_size = 1)(x)\n",
    "x = Dropout(0.2)(x)\n",
    "\n",
    "print(x.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "f2be0371",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Layer lstm_7 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.\n",
      "WARNING:tensorflow:Layer lstm_7 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.\n",
      "WARNING:tensorflow:Layer lstm_7 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.\n",
      "(None, 128)\n"
     ]
    }
   ],
   "source": [
    "lstm_out = Bidirectional(LSTM(lstm_units, activation='relu'), name='lstm')(x)\n",
    "#lstm_out = LSTM(lstm_units,activation='relu')(x)\n",
    "print(lstm_out.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "1542a692",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 1., 0., 0.],\n",
       "       [1., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 1.],\n",
       "       ...,\n",
       "       [0., 0., 0., 0., 1.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [1., 0., 0., 0., 0.]], dtype=float32)"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "c92d2a43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model_7\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "input_8 (InputLayer)         [(None, 5, 1)]            0         \n",
      "_________________________________________________________________\n",
      "conv1d_15 (Conv1D)           (None, 1, 128)            768       \n",
      "_________________________________________________________________\n",
      "max_pooling1d_7 (MaxPooling1 (None, 1, 128)            0         \n",
      "_________________________________________________________________\n",
      "dropout_7 (Dropout)          (None, 1, 128)            0         \n",
      "_________________________________________________________________\n",
      "lstm (Bidirectional)         (None, 128)               98816     \n",
      "_________________________________________________________________\n",
      "dense_7 (Dense)              (None, 5)                 645       \n",
      "=================================================================\n",
      "Total params: 100,229\n",
      "Trainable params: 100,229\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "output = Dense(5, activation='softmax')(lstm_out)\n",
    "#output = Dense(10, activation='sigmoid')(drop2)\n",
    "\n",
    "model = Model(inputs=inputs, outputs=output)\n",
    "print(model.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "40c8f50c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\ProgramData\\Miniconda3\\lib\\site-packages\\tensorflow\\python\\keras\\optimizer_v2\\optimizer_v2.py:374: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Could not interpret optimizer identifier: <tensorflow.python.keras.optimizer_v2.gradient_descent.SGD object at 0x0000021811940700>",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mf:\\swap\\My\\cnn-lstm分类.ipynb Cell 14'\u001b[0m in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/f%3A/swap/My/cnn-lstm%E5%88%86%E7%B1%BB.ipynb#ch0000013?line=0'>1</a>\u001b[0m sgd \u001b[39m=\u001b[39m SGD(lr\u001b[39m=\u001b[39m\u001b[39m0.01\u001b[39m)\n\u001b[1;32m----> <a href='vscode-notebook-cell:/f%3A/swap/My/cnn-lstm%E5%88%86%E7%B1%BB.ipynb#ch0000013?line=2'>3</a>\u001b[0m model\u001b[39m.\u001b[39;49mcompile(loss\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mcategorical_crossentropy\u001b[39;49m\u001b[39m'\u001b[39;49m, optimizer\u001b[39m=\u001b[39;49msgd, metrics\u001b[39m=\u001b[39;49m[\u001b[39m'\u001b[39;49m\u001b[39maccuracy\u001b[39;49m\u001b[39m'\u001b[39;49m])\n",
      "File \u001b[1;32mc:\\ProgramData\\Miniconda3\\lib\\site-packages\\keras\\engine\\training.py:548\u001b[0m, in \u001b[0;36mModel.compile\u001b[1;34m(self, optimizer, loss, metrics, loss_weights, weighted_metrics, run_eagerly, steps_per_execution, **kwargs)\u001b[0m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=544'>545</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_validate_compile(optimizer, metrics, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=545'>546</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_run_eagerly \u001b[39m=\u001b[39m run_eagerly\n\u001b[1;32m--> <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=547'>548</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39moptimizer \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_get_optimizer(optimizer)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=548'>549</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcompiled_loss \u001b[39m=\u001b[39m compile_utils\u001b[39m.\u001b[39mLossesContainer(\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=549'>550</a>\u001b[0m     loss, loss_weights, output_names\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_names)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=550'>551</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcompiled_metrics \u001b[39m=\u001b[39m compile_utils\u001b[39m.\u001b[39mMetricsContainer(\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=551'>552</a>\u001b[0m     metrics, weighted_metrics, output_names\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_names,\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=552'>553</a>\u001b[0m     from_serialized\u001b[39m=\u001b[39mfrom_serialized)\n",
      "File \u001b[1;32mc:\\ProgramData\\Miniconda3\\lib\\site-packages\\keras\\engine\\training.py:586\u001b[0m, in \u001b[0;36mModel._get_optimizer\u001b[1;34m(self, optimizer)\u001b[0m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=582'>583</a>\u001b[0m       opt \u001b[39m=\u001b[39m lso\u001b[39m.\u001b[39mLossScaleOptimizerV1(opt, loss_scale)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=583'>584</a>\u001b[0m   \u001b[39mreturn\u001b[39;00m opt\n\u001b[1;32m--> <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=585'>586</a>\u001b[0m \u001b[39mreturn\u001b[39;00m tf\u001b[39m.\u001b[39;49mnest\u001b[39m.\u001b[39;49mmap_structure(_get_single_optimizer, optimizer)\n",
      "File \u001b[1;32mc:\\ProgramData\\Miniconda3\\lib\\site-packages\\tensorflow\\python\\util\\nest.py:867\u001b[0m, in \u001b[0;36mmap_structure\u001b[1;34m(func, *structure, **kwargs)\u001b[0m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=862'>863</a>\u001b[0m flat_structure \u001b[39m=\u001b[39m (flatten(s, expand_composites) \u001b[39mfor\u001b[39;00m s \u001b[39min\u001b[39;00m structure)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=863'>864</a>\u001b[0m entries \u001b[39m=\u001b[39m \u001b[39mzip\u001b[39m(\u001b[39m*\u001b[39mflat_structure)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=865'>866</a>\u001b[0m \u001b[39mreturn\u001b[39;00m pack_sequence_as(\n\u001b[1;32m--> <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=866'>867</a>\u001b[0m     structure[\u001b[39m0\u001b[39m], [func(\u001b[39m*\u001b[39mx) \u001b[39mfor\u001b[39;00m x \u001b[39min\u001b[39;00m entries],\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=867'>868</a>\u001b[0m     expand_composites\u001b[39m=\u001b[39mexpand_composites)\n",
      "File \u001b[1;32mc:\\ProgramData\\Miniconda3\\lib\\site-packages\\tensorflow\\python\\util\\nest.py:867\u001b[0m, in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=862'>863</a>\u001b[0m flat_structure \u001b[39m=\u001b[39m (flatten(s, expand_composites) \u001b[39mfor\u001b[39;00m s \u001b[39min\u001b[39;00m structure)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=863'>864</a>\u001b[0m entries \u001b[39m=\u001b[39m \u001b[39mzip\u001b[39m(\u001b[39m*\u001b[39mflat_structure)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=865'>866</a>\u001b[0m \u001b[39mreturn\u001b[39;00m pack_sequence_as(\n\u001b[1;32m--> <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=866'>867</a>\u001b[0m     structure[\u001b[39m0\u001b[39m], [func(\u001b[39m*\u001b[39;49mx) \u001b[39mfor\u001b[39;00m x \u001b[39min\u001b[39;00m entries],\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/tensorflow/python/util/nest.py?line=867'>868</a>\u001b[0m     expand_composites\u001b[39m=\u001b[39mexpand_composites)\n",
      "File \u001b[1;32mc:\\ProgramData\\Miniconda3\\lib\\site-packages\\keras\\engine\\training.py:577\u001b[0m, in \u001b[0;36mModel._get_optimizer.<locals>._get_single_optimizer\u001b[1;34m(opt)\u001b[0m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=575'>576</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_get_single_optimizer\u001b[39m(opt):\n\u001b[1;32m--> <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=576'>577</a>\u001b[0m   opt \u001b[39m=\u001b[39m optimizers\u001b[39m.\u001b[39;49mget(opt)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=577'>578</a>\u001b[0m   \u001b[39mif\u001b[39;00m (loss_scale \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=578'>579</a>\u001b[0m       \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(opt, lso\u001b[39m.\u001b[39mLossScaleOptimizer)):\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/engine/training.py?line=579'>580</a>\u001b[0m     \u001b[39mif\u001b[39;00m loss_scale \u001b[39m==\u001b[39m \u001b[39m'\u001b[39m\u001b[39mdynamic\u001b[39m\u001b[39m'\u001b[39m:\n",
      "File \u001b[1;32mc:\\ProgramData\\Miniconda3\\lib\\site-packages\\keras\\optimizers.py:132\u001b[0m, in \u001b[0;36mget\u001b[1;34m(identifier)\u001b[0m\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/optimizers.py?line=129'>130</a>\u001b[0m   \u001b[39mreturn\u001b[39;00m deserialize(config)\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/optimizers.py?line=130'>131</a>\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m--> <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/optimizers.py?line=131'>132</a>\u001b[0m   \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[0;32m    <a href='file:///c%3A/ProgramData/Miniconda3/lib/site-packages/keras/optimizers.py?line=132'>133</a>\u001b[0m       \u001b[39m'\u001b[39m\u001b[39mCould not interpret optimizer identifier: \u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m.\u001b[39mformat(identifier))\n",
      "\u001b[1;31mValueError\u001b[0m: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizer_v2.gradient_descent.SGD object at 0x0000021811940700>"
     ]
    }
   ],
   "source": [
    "sgd = SGD(lr=0.01)\n",
    "\n",
    "model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36c5abc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#拟合模型\n",
    "history=model.fit(x_train, y_train, batch_size=50, epochs=300)#迭代次数要自己选择一下，应该问题不大但是迭代次数越多，时间越慢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91036909",
   "metadata": {},
   "outputs": [],
   "source": [
    "#预测\n",
    "#predictions = model.predict(x_test) \n",
    "#还原 特征缩放\n",
    "predictions =np.argmax(model.predict(x_test), axis=1)\n",
    "one_hot_test_labels1=np.argmax(y_test, axis=1)\n",
    "#predictions =model.predict(x_test)\n",
    "predictions\n",
    "one_hot_test_labels1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6abcc8d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions\n",
    "plt.plot(predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50c1e9cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import precision_score,recall_score\n",
    "precision= precision_score(one_hot_test_labels1,predictions,average='micro')\n",
    "recall = recall_score(one_hot_test_labels1, predictions,average='micro')\n",
    "print(\"precision_score\",precision)\n",
    "print(\"recall_scor\",recall)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb60b1e8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix, classification_report, roc_curve, auc\n",
    "\n",
    "print(\"Confusion matrix (validation):\\n {0}\\n\".format(confusion_matrix(one_hot_test_labels1, predictions)))\n",
    "print(\"Classification report (validation):\\n {0}\".format(classification_report(one_hot_test_labels1, predictions)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89e1f2f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,5))\n",
    "plt.plot(range(len(one_hot_test_labels1[0:2000])),predictions[0:2000],'b.',label=\"predict\")\n",
    "plt.plot(range(len(one_hot_test_labels1[0:2000])),predictions[0:2000],'r.',label=\"test\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "plt.xlabel('测试集编号', fontsize=18)\n",
    "#y轴\n",
    "plt.ylabel(\"分类编号\", fontsize=18)\n",
    "plt.savefig(\"./minist.jpg\")\n",
    "plt.show()\n",
    "#plt.savefig(\"./minist.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbf9d367",
   "metadata": {},
   "outputs": [],
   "source": [
    "history_dict=history.history\n",
    "history_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e91977a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,5))\n",
    "hist_dict=history.history\n",
    "loss_values=hist_dict[\"loss\"]\n",
    "val_acc=history_dict[\"accuracy\"]\n",
    "epochs=range(1,len(loss_values)+1)\n",
    "plt.plot(epochs,loss_values,\"b\",label=\"Training loss\")\n",
    "plt.xlabel('Epochs', fontsize=20)\n",
    "#y轴\n",
    "plt.ylabel(\"loss\", fontsize=22)\n",
    "plt.savefig(\"./epochs.jpg\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "656d8738",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(loss_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "614415c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "hist_dict=history.history\n",
    "loss_values=hist_dict[\"loss\"]\n",
    "val_acc=history_dict[\"accuracy\"]\n",
    "epochs=range(1,len(loss_values)+1)\n",
    "plt.plot(epochs,val_acc,\"b\",label=\"Training loss\")\n",
    "plt.xlabel('Epochs', fontsize=20)\n",
    "#y轴\n",
    "plt.ylabel(\"val_acc\", fontsize=20)\n",
    "plt.savefig(\"./ACC.jpg\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e7ad896",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n",
    "cm = confusion_matrix(y_true=one_hot_test_labels1, y_pred=predictions)\n",
    "#labels = one_hot_test_labels.target_names\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm)\n",
    "disp.plot()\n",
    "plt.savefig(\"./TU.jpg\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b046b33",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12,5))\n",
    "# 得到混淆矩阵(confusion matrix,简称cm)\n",
    "# confusion_matrix 需要的参数：y_true(真实标签),y_pred(预测标签),normalize(归一化,'true', 'pred', 'all')\n",
    "cm1 = confusion_matrix(y_true=one_hot_test_labels1, y_pred=predictions, normalize='true')\n",
    "disp1 = ConfusionMatrixDisplay(confusion_matrix=cm1)\n",
    "disp1.plot()\n",
    "plt.savefig(\"./s.jpg\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80265480",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c41eda7b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37819ef0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "19d1d53a962d236aa061289c2ac16dc8e6d9648c89fe79f459ae9a3493bc67b4"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
